<?php
//Login ticket function library
function ticketStatus($uName)
{
	$tables = array();
	$tables[0] = 'extras_loginTicket';
	
	$fields = array();
	$fields[0] = 'ticket';
	
	$whereClause = 'userName="'.$uName.'"';
	
	$data = SQLRecover($tables, $fields, $whereClause);
	
	if (!isset($data['ticket'][0]) || sizeOf($data['ticket']) < 1)
	{
		return false;
	}
	else
	{
		$var = ($data['ticket'] == 'kill' ? false : true);
		return $var;
	}
}

function generateTicket($uName)
{
	$tick = crypt(time());
	$tables = array();
	$tables[0] = 'extras_loginTicket';
	
	$fields = array();
	$fields[0] = 'userName';
	
	$whereClause = 'userName = "'.$uName.'"';
	
	$check = SQLRecover($tables, $fields, $whereClause);
	
	if ($check['queryStatus'] == 'DB Query success!' && sizeOf($check['userName']) > 0)
	{
		//if exists
		$_SESSION['ticket'] = $tick;
	
		$tableName = 'extras_loginTicket';
		
		$fields = array();
		$fields[0] = 'ticket';
		
		$values = array();
		$values[0] = $tick;
		
		$whereClause = 'userName="'.$uName.'"';
		
		$data = SQLUpdate($tableName, $whereClause, $fields, $values);
		
		if ($data['queryStatus'] == "The query was saved successfully.")
		{
			$_SESSION['ticket'] = $tick;
			return true;
		}
		else
		{
			return false;
		}
		
	}
	else
	{
		//if Not Exist
	
		$tableName = 'extras_loginTicket';
		
		$fields = array();
		$fields[0] = 'userName';
		$fields[1] = 'ticket';
		
		$values = array();
		$values[0] = $uName;
		$values[1] = $tick;
		
		$data = SQLInsert($tableName, $fields, $values);
		
		if ($data['queryStatus'] == "The query was saved successfully.")
		{
			$_SESSION['ticket'] = $tick;
			return true;
		}
		else
		{
			return false;
		}
	}
	
		
}

function checkTicket($uName)
{
	if (session('ticket') != null)
	{
		$tables = array();
		$tables[0] = 'extras_loginTicket';
		
		$fields = array();
		$fields[0] = 'ticket';
		
		$whereClause = 'userName="'.$uName.'"';
		
		$data = SQLRecover($tables, $fields, $whereClause);
		
		if(isset($data['ticket'][0]) && session('ticket') == $data['ticket'][0])
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

function closeTicket($userName)
{
	$tableName = 'extras_loginTicket';
	
	$fields = array();
	$fields[0] = 'ticket';
	
	$values = array();
	$values[0] = 'kill';
	
	$whereClause = 'userName="'.$userName.'"';
	
	$data = SQLUpdate($tableName, $whereClause, $fields, $values);
	
	return $data['traceSQL'];
}
?>